package com.myProject.serves.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TArea;
import com.myProject.bean.TThing;
import com.myProject.dao.AreaDao;
import com.myProject.dao.ThingDao;
import com.myProject.serves.ThingSev;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionUtil;

import java.util.List;

public class ThingSevImpl implements ThingSev {
    @Override
    public List<TThing> getThingList(TThing tThing) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThingDao td = sqlSession.getMapper(ThingDao.class);
        List<TThing> list = td.getThingList(tThing);
        sqlSession.clearCache();
        return list;
    }

    @Override
    public PageInfo<TThing> showThingByPage(int pageNo, int pageSize, TThing tThing) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThingDao td = sqlSession.getMapper(ThingDao.class);
        PageHelper.startPage(pageNo, pageSize);
        List<TThing> list = td.getThingList(tThing);
        PageInfo<TThing> page = new PageInfo<TThing>(list);
        sqlSession.clearCache();
//      sqlSession.close();
        return page;
    }

    @Override
    public List<TThing> needHelp(TThing tThing) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThingDao td = sqlSession.getMapper(ThingDao.class);
        List<TThing>list = td.needHelp(tThing);
        sqlSession.clearCache();
        return list;
    }

    @Override
    public PageInfo<TThing> helpByPage(int PageNo, int pageSize, TThing tThing) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThingDao td = sqlSession.getMapper(ThingDao.class);
        PageHelper.startPage(PageNo, pageSize);
        List<TThing> list = td.needHelp(tThing);
        PageInfo<TThing> page = new PageInfo<TThing>(list);
        SqlSessionUtil.commit();
//      sqlSession.close();
        return page;
    }

    @Override
    public boolean addThing(TThing tThing) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThingDao td = sqlSession.getMapper(ThingDao.class);
        int x =td.addThing(tThing);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x>0?true:false;
    }

    @Override
    public boolean updateThing(TThing tThing) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThingDao td = sqlSession.getMapper(ThingDao.class);
        int x = td.updateThing(tThing);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x>0?true:false;
    }
}
